package cc.sheji.reposity;

import cc.sheji.domain.Job;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

@Repository
public interface JobRepository  extends CrudRepository<Job, Long>, JpaSpecificationExecutor<Job> {




    public Job findFirstByStateAndProductModeOrderByUpdateTimeAsc(int state,int productMode);

    public Job findFirstByStateAndPlateUserIdAndProductModeOrderByUpdateTimeAsc(int state, String plateUserId,int productMode);

    List<Job> findAllByState(int state);
    List<Job> findAllByStateAndPlateUserId(int state,String plateUserId);

    @Modifying
    @Query(value = "update job set notice_state=0,  state =10 ,error_state=1 where state=1 and  result IS NULL and  finish_time<= DATE_SUB( NOW(), INTERVAL 60 MINUTE )  " ,nativeQuery = true)
    void updateJobsByStateAndFinalResult();


    @Modifying
    @Query(value = "update job set notice_state=0,  state =11 ,error_state=1 where state=5 and  result IS NULL and  update_time<= DATE_SUB( NOW(), INTERVAL 300 MINUTE )  " ,nativeQuery = true)
    void updateJobsByStateAndFinalResult1();

    @Modifying
    @Query(value = "update job set notice_state=0,  state =11 ,error_state=1 where state=6 and  result IS NULL and  update_time<= DATE_SUB( NOW(), INTERVAL 300 MINUTE )  " ,nativeQuery = true)
    void updateJobsByStateAndFinalResult2();

    @Modifying
    @Query(value = "update job set notice_state=0,  state =11 ,error_state=1 where state=7 and  result IS NULL and  update_time<= DATE_SUB( NOW(), INTERVAL 300 MINUTE )  " ,nativeQuery = true)
    void updateJobsByStateAndFinalResult3();


    @Query(value = "SELECT id FROM job  where state=1 and  result IS NULL and  finish_time<= DATE_SUB( NOW(), INTERVAL 55 MINUTE )  " ,nativeQuery = true)
    List<Long> getJobIdsByStateAndFinalResult();
//    @Modifying
//    @Query(value = "update job set   state =10 ,error_state=1 where state=5 and  result IS NULL and  finish_time<= DATE_SUB( NOW(), INTERVAL 60 MINUTE )  " ,nativeQuery = true)
//    void updateJobsByStateAndFinalResult5();
//    @Modifying
//    @Query(value = "update job set   state =10 ,error_state=1 where state=6 and  result IS NULL and  finish_time<= DATE_SUB( NOW(), INTERVAL 60 MINUTE )  " ,nativeQuery = true)
//    void updateJobsByStateAndFinalResult6();
//    @Modifying
//    @Query(value = "update job set   state =10 ,error_state=1 where state=7 and  result IS NULL and  finish_time<= DATE_SUB( NOW(), INTERVAL 60 MINUTE )  " ,nativeQuery = true)
//    void updateJobsByStateAndFinalResult7();

    List<Job> findAllByErrorStateAndNoticeState(Integer errorState,Integer noticeState);

    @Query(value = "SELECT * FROM job  where state!=4 and state!=10 AND state != 3 AND task_id!='' and notice_state is NULL ORDER BY create_time desc " ,nativeQuery = true)
    List<Job> findAllByTaskState();
    @Query(value = "SELECT * FROM job  where  `state` <> '4' AND `state` <> '3' AND `state` <> '10' AND `state` <> '9' AND `state` <> '11' AND `state` <> '13' AND `state` <> '12'  and ISNULL(task_id) ORDER BY create_time desc " ,nativeQuery = true)
        List<Job> findNewByTaskState();
}
